<style>
    .layui-btn{margin:3px;}
    .passwords{cursor:pointer;}
</style>

<div style="margin-left: 94%;">
    <a class="layui-btn layui-btn-primary layui-btn-sm operate" operate-type="add"><i class="layui-icon">&#xe608;</i>添加</a>
</div>

<table class="layui-table">
    <thead>
    <tr>
        <th width="5%;">ID</th>
        <th width="10%;">QQ号码</th>
        <th width="10%;">昵称</th>
        <th width="10%;">登陆密码</th>
        <th width="10%;">角色密码</th>
        <th width="10%;">仓库密码</th>
        <th width="10%;">是否公开</th>
        <th width="10%;">是否可跳</th>
        <th width="10%;">操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<!--分页start-->
<div id="paginate"></div>
<!--分页end-->

<script>
    $(function(){
        // GetTableList();
    });

    //获取表格数据
    function GetTableList(page=1,pagesize=10){
        var url = '/Handler/Trumpet/GetLineList';
        var data = {page:page,pagesize:pagesize};
        dink.post(url,data,function(res){
            if(res.ret == 1){
                var target = $('tbody');
                var html = '';
                $.each(res.data,function(i,ele){
                    html += '<tr>\n' +
                        '    <td>'+(i+1)+'</td>\n' +
                        '    <td>'+ele.account+'</td>\n' +
                        '    <td>'+ele.nickname+'</td>\n' +
                        '    <td class="passwords" data-hide="'+ele.hide1+'">'+(ele.password_login ? '******' : '')+'</td>\n' +
                        '    <td class="passwords" data-hide="'+ele.hide2+'">'+(ele.password_role ? '******' : '')+'</td>\n' +
                        '    <td class="passwords" data-hide="'+ele.hide3+'">'+(ele.password_storage ? '******' : '')+'</td>\n' +
                        '    <td>'+(ele.is_public==1 ? '' : '<i class="layui-icon" style="color:blue;font-size:20px;font-weight:bolder;">&#xe605;</i>')+'</td>\n' +
                        '    <td>'+(ele.is_used==2 ? '' : '<i class="layui-icon" style="color:blue;font-size:20px;font-weight:bolder;">&#xe605;</i>')+'</td>\n';
                    if(res.uid ==0 || res.uid == ele.uid){
                        html += '<td>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="edit" data-id="'+ele.id+'">编辑</a>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="delete" data-id="'+ele.id+'">删除</a>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }else{
                        html += '<td>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }
                });
                target.html(html);
                if(page == 1){
                    Paginate(res.total,pagesize);
                }
            }else{
                var target = $('tbody');
                var html = '<tr><td colspan="9">无数据</td></tr>';
                target.html(html);
                layer.msg(res.msg);return;
            }
        });
    }

    //分页
    function Paginate(count,pagesize){
        layui.use('laypage',function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'paginate',
                count: count,
                limit: pagesize,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj,first){
                    if(!first){
                        GetTableList(obj.curr,pagesize);
                    }
                }
            });
        });
    }

    //操作按钮click事件
    dink.click('.operate',function(){
        var operate_type = $(this).attr('operate-type');
        var id = $(this).attr('data-id');
        //判断权限
        dink.post('/Handler/Permission/CheckOperatePermission',{permission:'trumpet:line:'+operate_type},function(res){
            if(res.ret == 1){
                AdminTrumpetLineOpreate(operate_type,id);
            }else{
                layer.msg(res.msg);return;
            }
        });
        //end
    });

    //添加/编辑项目
    function AdminTrumpetLineOpreate(operate='add',id=0){
        if(operate == 'add'){
            dink.html('AdminTrumpetLineAdd','添加行脚号',['400px','520px']);
        }else if(operate == 'edit'){
            dink.html('AdminTrumpetLineAdd','编辑行脚号',['400px','520px']);
            setTimeout(function(){
                dink.post('/Handler/Trumpet/GetLineDetailById',{id:id},function(res){
                    var item = res.data[0];
                    $('input[name=nickname]').val(item.nickname);
                    $('input[name=account]').val(item.account);
                    // $('input[name=password_login]').val(item.password_login);
                    // $('input[name=password_role]').val(item.password_role);
                    // $('input[name=password_storage]').val(item.password_storage);
                    $('input[name=is_used][value='+item.is_used+']').prop('checked',true);
                    $('input[name=is_public][value='+item.is_public+']').prop('checked',true);
                    $('input[name=nickname]').attr('disabled',true);
                    $('input[name=account]').attr('disabled',true);
                    $('input[name=password_login]').attr('placeholder','不填写则默认不修改密码');
                    $('input[name=password_role]').attr('placeholder','不填写则默认不修改密码');
                    $('input[name=password_storage]').attr('placeholder','不填写则默认不修改密码');
                });
            },50);
        }else if(operate == 'delete'){
            dink.alert('Tips','确定删除？',function(){
                dink.post('/Handler/Trumpet/DeleteLine',{id:id},function(res){
                    layer.msg(res.msg);
                    GetTableList();return;
                });
            });
        }else{
            layer.msg('操作类型异常');return;
        }
        //添加、编辑
        dink.click('#add',function(){
            var data = {};
            var base = {};
            if(operate == 'add'){
                var url = '/Handler/Trumpet/AddLine';
            }else if(operate == 'edit'){
                var url = '/Handler/Trumpet/EditLine';
                if(id == 0){
                    layer.msg('缺少记录id');return;
                }
                data.id = id;
            }else{
                layer.msg('操作类型异常');return;
            }
            base.nickname = $('input[name=nickname]').val();
            base.account = $('input[name=account]').val();
            if($('input[name=password_login]').val() != ''){
                base.password_login = $('input[name=password_login]').val();
            }
            if($('input[name=password_role]').val() != ''){
                base.password_role = $('input[name=password_role]').val();
            }
            if($('input[name=password_storage]').val() != ''){
                base.password_storage = $('input[name=password_storage]').val();
            }
            base.is_used = $('input[name=is_used]:checked').val();
            base.is_public = $('input[name=is_public]:checked').val();
            data.base = base;
            if(base.account == ''){
                layer.msg('请填写QQ号码');return;
            }
            dink.alert('Tips','确定'+(operate=='add' ? '添加' : '编辑')+'行脚号？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });
    }

    //显示、隐藏密码
    dink.click('.passwords',function(){
        var text = $(this).text();
        var hide = $(this).attr('data-hide');
        if(text == '******'){
            $(this).text(hide);
        }else if(text != ''){
            $(this).text('******');
        }else{
            //值不存在
        }
    })
</script>